package battleship;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

public class CreateDatabase {

		public static void create() {
		
			 try {
				 Statement stmt;
				 ResultSet rs;
				
				 //Register the JDBC driver for MySQL.
				 Class.forName("com.mysql.jdbc.Driver");
				 
//				 String url =
//						 "jdbc:mysql://localhost:3306/JunkDB";
				 
//				 Connection con =
//						 (Connection) DriverManager.getConnection(
//				 url,"auser", "drowssap");
				 
				 String url =
						 "jdbc:mysql://localhost:3306/mysql";
				 Connection con =
						 (Connection) DriverManager.getConnection(
				 url,"root", "");
				 
			//	 String url =  "jdbc:mysql://mysql-tartubattleship.jelastic.dogado.eu/junkdb";
				 
			//	 String url =  "jdbc:mysql://mysql-tartubattleship.jelastic.dogado.eu/mysql";
			//	 Connection con = (Connection) DriverManager.getConnection(url, "root", "5mfITFh1rb");
				 
			//	 Connection con = (Connection) DriverManager.getConnection(url, "kasutaja", "dbparool1");
				 //Display URL and connection information
				 System.out.println("URL: " + url);
				 System.out.println("Connection: " + con);

				 //Get a Statement object
				 stmt = (Statement) con.createStatement();
//				 stmt.executeUpdate("DROP TABLE JunkDB");
				 
				 try {
					System.out.println("Create a new database...");
					stmt.executeUpdate("CREATE DATABASE JunkDB;");
				 }catch (SQLException e) {
						System.out.println("SQLException: " + e.getMessage());
						System.out.println("SQLState: "+ e.getSQLState());
						System.out.println("VendorError: " + e.getErrorCode());
					 
				 }
				 
//				 try {
//					System.out.println("Create a new database...");
//					stmt.executeUpdate("CREATE DATABASE JunkDB;");
//				 }catch (SQLException e) {
//						System.out.println("SQLException: " + e.getMessage());
//						System.out.println("SQLState: "+ e.getSQLState());
//						System.out.println("VendorError: " + e.getErrorCode());
//					 
//				 }
	
				 try {
					System.out.println("Use the new database...");
					stmt.executeUpdate("USE JunkDB;");

                     // kristina lisatud
                     stmt.executeUpdate(
                             "GRANT SELECT,INSERT,UPDATE,DELETE," +
                                     "CREATE,DROP " +
                                     "ON JunkDB.* TO 'auser'@'localhost' " +
                                     "IDENTIFIED BY 'drowssap';");
                     // kristina lisatud

				 }catch (SQLException e) {
					 stmt.executeUpdate(
							 "GRANT SELECT,INSERT,UPDATE,DELETE," +
							 "CREATE,DROP " +
							 "ON JunkDB.* TO 'auser'@'localhost' " +
							 "IDENTIFIED BY 'drowssap';");
//					 "ON JunkDB.* TO 'root'@'mysql-tartubattleship.jelastic.dogado.eu/' " +
//					 "IDENTIFIED BY '5mfITFh1rb';");
				 }
				 try {
					String table = "CREATE TABLE testtable( "
							+ "name VARCHAR(255)," + "winpoints INTEGER," +  "losepoints INTEGER" +");";
					System.out.println("Create a new table in the new database...");
					stmt.executeUpdate(table);
			 }catch (SQLException e) {
					System.out.println("SQLException: " + e.getMessage());
					System.out.println("SQLState: "+ e.getSQLState());
					System.out.println("VendorError: " + e.getErrorCode());
			 }
				 
			} catch (Exception e) {
			}
		}

	
}
